﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data.SqlClient;
using System.Data;

namespace DAO
{
    public class KhachHangDA
    {
        
            public static KhachHang GetById(int maKH)
            {
                KhachHang result = null;

                SqlConnection cn = DataProvider.Connect();
                SqlCommand cmd = new SqlCommand("Proc_KhachHang_GetById", cn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add(new SqlParameter("@maKH", maKH));

                SqlDataReader dataReader = cmd.ExecuteReader();
                if (dataReader.Read())
                {

                    result = new KhachHang();
                    result.MaKH = maKH;
                    result.Ten = Convert.ToString(dataReader["Ten"]);
                    result.CMND = Convert.ToInt32(dataReader["CMND"]);
                    result.PassPost = Convert.ToString(dataReader["PassPost"]);
                    result.NgaySinh = Convert.ToDateTime(dataReader["NgaySinh"]);
                    result.DiaChi = Convert.ToString(dataReader["DiaChi"]);
                    if (dataReader["MaSoThue"].ToString() == "")
                    {
                        result.MaSoThue = null;
                    }
                    else
                    {
                        result.MaSoThue = Convert.ToInt32(dataReader["MaSoThue"]);
                    }
                    result.GiayPhepKinhDoanh = Convert.ToString(dataReader["GiayPhepKinhDoanh"]);
                    result.MaLoaiKH = Convert.ToInt32(dataReader["MaLoaiKH"]);
                    result.NguoiDaiDien = Convert.ToString(dataReader["NguoiDaiDien"]);
                    result.MaTruSo = Convert.ToInt32(dataReader["MaTruSo"]);
                    result.SoDT = Convert.ToString(dataReader["SoDT"]);
                    result.TruSo = TruSoDA.GetByID(result.MaTruSo);
                    result.LoaiKH = LoaiKHDA.GetByID(result.MaLoaiKH);
                    //result.TruSo = TruSoDA.GetByID(result.MaTruSo);
                }
                cn.Close();
                return result;
            }

            public static void Update(KhachHang khachHang)
            {
                SqlConnection cn = DataProvider.Connect();
                SqlCommand cmd = new SqlCommand("Proc_KhachHang_Update", cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maKH", khachHang.MaKH));
                cmd.Parameters.Add(new SqlParameter("@ten", khachHang.Ten));
                cmd.Parameters.Add(new SqlParameter("@cmnd", khachHang.CMND));
                cmd.Parameters.Add(new SqlParameter("@passPost", khachHang.PassPost));
                cmd.Parameters.Add(new SqlParameter("@ngaySinh", khachHang.NgaySinh));
                cmd.Parameters.Add(new SqlParameter("@diaChi", khachHang.DiaChi));
                cmd.Parameters.Add(new SqlParameter("@maSoThue", khachHang.MaSoThue));
                cmd.Parameters.Add(new SqlParameter("@giayphep", khachHang.GiayPhepKinhDoanh));
                cmd.Parameters.Add(new SqlParameter("@nguoiDaiDien", khachHang.NguoiDaiDien));
                cmd.Parameters.Add(new SqlParameter("@SoDT", khachHang.SoDT));
                cmd.ExecuteNonQuery();
                cn.Close();
            }

            //public static List<KhachHang> GetALl()
            //{
            //    List<KhachHang> dsKhachHang = new List<KhachHang>();
            //    SqlConnection cn = DataProvider.Connect();
            //    SqlCommand cmd = new SqlCommand("sp_DanhSachKhachHang", cn);
            //    cmd.CommandType = CommandType.StoredProcedure;
            //    SqlDataReader dataReader = cmd.ExecuteReader();
            //    while (dataReader.Read())
            //    {
            //        KhachHang result = new KhachHang();
            //        result.MaKH = Convert.ToInt32(dataReader["MaKH"]);
            //        result.Ten = Convert.ToString(dataReader["Ten"]);
            //        result.CMND = Convert.ToInt32(dataReader["CMND"]);
            //        result.PassPost = Convert.ToString(dataReader["PassPost"]);
            //        result.NgaySinh = Convert.ToDateTime(dataReader["NgaySinh"]);
            //        result.DiaChi = Convert.ToString(dataReader["DiaChi"]);
            //        if (dataReader["MaSoThue"].ToString() == "")
            //        {
            //            result.MaSoThue = null;
            //        }
            //        else
            //        {
            //            result.MaSoThue = Convert.ToInt32(dataReader["MaSoThue"]);
            //        }
            //        result.GiayPhepKinhDoanh = Convert.ToString(dataReader["GiayPhepKinhDoanh"]);
            //        result.MaLoaiKH = Convert.ToInt32(dataReader["MaLoaiKH"]);
            //        result.NguoiDaiDien = Convert.ToString(dataReader["NguoiDaiDien"]);
            //        result.MaTruSo = Convert.ToInt32(dataReader["MaTruSo"]);
            //        result.SoDT = Convert.ToString(dataReader["SoDT"]);
            //        result.TruSo = TruSoDA.GetByID(result.MaTruSo);
            //        result.LoaiKH = LoaiKHDA.GetByID(result.MaLoaiKH);
            //        result.TruSo = TruSoDA.GetByID(result.MaTruSo);
            //        dsKhachHang.Add(result);
            //    }
            //    cn.Close();
            //    return dsKhachHang;
            //}
    }
    
}
